/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/


/**
	@module jgrouse.widgets.StaticText
	@requires jgrouse.widgets.Widget
*/
jgrouse.module(
{
	name : 'jgrouse.widgets.StaticText',
	requires : ['jgrouse.widgets.Widget'],
	body : function(embed)
	{
		eval(embed);
		
		jgrouse.widgets.registerWidget('staticText', 'jgrouse.widgets.StaticText');
		
		/**
			Widget representing text. Registered under "staticText" alias.
			Exported properties:
			<ul>
				<li>value</li>
			</ul>
			@class jgrouse.widgets.StaticText
			@extends jgrouse.widgets.Widget
		*/
		jgrouse.define('jgrouse.widgets.StaticText', 'jgrouse.widgets.Widget', function(salt)
		{
			eval(salt);
			
			return {
				/**
					@constructor initialize
					@param {Object} options
					@param {Object} styles
					@param {Object} text
				*/
				initialize : function(options, styles, text)
				{
					_super(this, 'initialize', options, styles);
					/**
						Text to be rendered
						@variable {String} _value
					*/
					this._value = text;
					/**
						TextNode that is inserted within DOM element
						@variable {TextNode} _textNode
					*/
					this._textNode = null;
				},
				
				/**
					@function render
					@inheritdesc
				*/
				render : function()
				{
					_super(this, 'render');
					if (this._value)
					{
						this._textNode = document.createTextNode(this._value);
						this._domElement.appendChild(this._textNode);
					}
				},
				
				/**
					Set name of tag that contains text (i.e. div, span, etc)
					@function setTagName
					@param {String} tagName
				*/
				setTagName : function(tagName)
				{
					this._tagName = tagName;
				},
				
				/**
					Set template for container. 
					@function setTemplate
					@paramset Set template string
					@param {String} template - string with HTML text of the template
					@paramset Set template parser
					@param {TemplateParser} template - parent template
				*/
				setTemplate : function(template)
				{
					if (typeof template == 'string')
					{
						this.setValue(template)
					}
					else
					{
						this.setValue(template._currentNode.innerHTML);
						template._currentNode.innerHTML = '';
					}
				},
				
				/**
					Change displayed text by setting the <b>value</b> property
					@function setValue
					@param {String} value - text to display
				*/
				setValue : function(value)
				{
					this._value = value;
					if (this._domElement)
					{
						if (value)
						{
							if (this._textNode)
							{
								this._textNode.data = value;
							}
							else
							{
								this._textNode = document.createTextNode(value);
								this._domElement.appendChild(this._textNode);
							}
						}
						else
						{
							if (this._textNode)
							{
								this._domElement.removeChild(this._textNode);
								this._textNode = null;
							}
						}
					}
				},
				
				/**
					Returns <b>value</b> property
					@function {String} getValue
				*/
				getValue : function()
				{
					return this._value;
				}
				
			}
		});
		
		
		jgrouse.widgets.registerWidget('link', 'jgrouse.widgets.Link');
		/**
			Class representing HTML link. Equivalent to &lt;a&gt;. Registered under <code>link</code> alias
			@class jgrouse.widgets.Link
			@extends jgrouse.widgets.StaticText
		*/
		jgrouse.define('jgrouse.widgets.Link', 'jgrouse.widgets.StaticText', function(embed)
		{
			eval(embed);
			
			return {
				
				initialize : function(options, styles, text)
				{
					_sup(this, arguments);
					this._realRef = null;
					/**
					 * Indicator if clicking on link should add link's URL to history. Default value is "false"
					 * @variable {boolean} _addToHistory
					 */
					this._addToHistory= false;
				},
				
				setRealRef : function(href)
				{
					this.setAttribute('href', href);
				},
				
				
				/**
					If clickValue property has not been specified, then <code>href</code> would be used instead
					@function {String} getClickValue
					@see jgrouse.widgets.Widget.getClickValue
				*/
				getClickValue : function()
				{
					var v = _sup(this, arguments);
					if (typeof v == 'undefined')
					{
						v = this.getAttribute('href');
					}
					return v;
				},
				
				/**
				 * Indicates if clicking on link creates an entry in history
				 * @function setAddToHistory
				 * @param {boolean} value - true if clicking on link adds link's url to history 
				 */
				setAddToHistory : function(value)
				{
					this._addToHistory = value;
				},
				
				/**
				 * @function onClick
				 * @inheritdesc
				 */
				onClick : function(event)
				{
					_sup(this, arguments);
					var clickValue = this.getClickValue();
					if (this._suppressDefault['click'] && this._addToHistory)
					{
						jgrouse.browser.instance.setHistoryEntry(clickValue);
					}
				}
			}
		});
		
		/**
		 * Smart link - the href of link is being replaced with #&lt;original link&gt;. 
		 * Suppresses default handling of click event and adds clicked links to history. Registered 
		 * under <b>slink</b> alias
		 * @class jgrouse.widgets.SLink
		 * @extends jgrouse.widgets.Link
		 */
		
		jgrouse.widgets.registerWidget('slink', 'jgrouse.widgets.SLink');
		jgrouse.define('jgrouse.widgets.SLink', 'jgrouse.widgets.Link', function(embed)
		{
			eval(embed);
			
			return {
				initialize : function(options, styles)
				{
					_sup(this, arguments);
					var href = options.href;
					this.setClickValue(jgrouse.browser.instance.normalizeAncor(href));
					this.setAttribute('href', '#' + escape(href));
					this.setAddToHistory(true);
					this._suppressDefault['click'] = true;
				}
			}
			
		});
	}
});